package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsUser;
import com.qf.pojo.DtsUserAccount;
import com.qf.service.UserAccountService;
import com.qf.service.UserService;
import com.qf.util.JacksonUtil;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserAccountService userAccountService;


    //http://localhost:8080/admin/user/list?page=1&limit=20&username=asdf&mobile=564654&sort=add_time&order=desc
    @RequiresPermissions(value = "admin:user:list")
    @RequiresPermissionsDesc(menu = {"用户管理","会员管理"},button = "查询")
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit,
                       String username,String mobile,String sort,String order){

        IPage<DtsUser> iPage=userService.findUserByQuery(page,limit,username,mobile,sort,order);

        Map data=new HashMap();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    //http://localhost:8080/admin/user/detailApprove?id=66
    @RequiresPermissions(value = "admin:user:detailApprove")
    @RequiresPermissionsDesc(menu = {"用户管理","会员管理"},button = "推广代理")
    @GetMapping("/detailApprove")
    public Object detailApprove(Integer id){
        DtsUserAccount dtsUserAccount = userAccountService.findByUserId(id);
        return ResponseUtil.ok(dtsUserAccount);
    }

    @RequiresPermissions(value = "admin:user:approveAgency")
    @RequiresPermissionsDesc(menu = {"用户管理", "会员管理"}, button = "审批")
    @PostMapping("/approveAgency")
    public Object approveAgency(@RequestBody String json) {
        Integer userId = JacksonUtil.parseInteger(json, "userId");
        Integer settlementRate = JacksonUtil.parseInteger(json, "settlementRate");
        userAccountService.settlementRate(userId, settlementRate);
        return ResponseUtil.ok();
    }


}
